package lotus.blog.lotusblog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lotus.blog.lotusblog.mapper.UserMapper;
import lotus.blog.lotusblog.pojo.User;
import lotus.blog.lotusblog.server.Mail.MailService;
import lotus.blog.lotusblog.server.UserServiceImpl;
import lotus.blog.lotusblog.utils.JwtUtil;
import lotus.blog.lotusblog.utils.ResultTemplate;
import lotus.blog.lotusblog.utils.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController()
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserServiceImpl userService;
    @Autowired
    MailService mailService;
    @Autowired
    JwtUtil jwtUtil;
    @PostMapping("/register")
    public ResultTemplate register(@RequestBody User user){
        if(!Util.checkUserValid(user)){
            return ResultTemplate.fail("格式错误");
        }
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.eq("username",user.getUsername());
        User queryUser = userMapper.selectOne(wrapper);
        if (queryUser != null){
            return ResultTemplate.fail("用户名已存在");
        }
        wrapper.clear();
        wrapper.eq("email",user.getEmail());
        queryUser = userMapper.selectOne(wrapper);
        if (queryUser != null){
            return ResultTemplate.fail("邮箱已注册过");
        }
        try {
            mailService.sendSampleEmail("感谢您的注册","注册成功",user.getEmail());
        }catch (Exception e){
            return ResultTemplate.fail("非法邮箱");
        }
        userMapper.insert(user);
        return ResultTemplate.succ("注册成功",null);
    }
    @PostMapping("/login")
    public ResultTemplate login(@RequestBody User user, HttpServletResponse response){
        //用户验证
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.eq("email",user.getEmail());
        User queryUser = userMapper.selectOne(wrapper);
        if (queryUser == null){
            return ResultTemplate.fail("邮箱不存在");
        }else{
            if (!queryUser.getPassword().equals(user.getPassword())){
                return ResultTemplate.fail("密码错误");
            }
        }
        //签发token
        String token = jwtUtil.getJwtToken(user.getEmail());
        response.setHeader("Authorization", token);
        response.setHeader("Access-Control-Expose-Headers", "Authorization");
        queryUser.setPassword(null);
        return ResultTemplate.succ("登陆成功",queryUser);
    }
    @GetMapping("/getUserInfoByToken")
    public ResultTemplate getUserInfo(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String email = jwtUtil.getUserClaim(token);
        User queryUser = userMapper.getUserByEmail(email);
        queryUser.setPassword(null);
        return ResultTemplate.succ("获取数据成功",queryUser);
    }
    @GetMapping("/getUserInfoByUsername")
    public ResultTemplate get(@RequestParam("username") String username){ ;
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.eq("username",username);
        User queryUser = userMapper.selectOne(wrapper);
        queryUser.setPassword(null);
        if (queryUser ==null){
            return ResultTemplate.fail("用户不存在");
        }
        return ResultTemplate.succ("获取数据成功",queryUser);
    }
    @PostMapping("/update")
    public ResultTemplate update(@RequestBody User user, HttpServletRequest request){
        User userBytoken = jwtUtil.getUserBytoken(request);
        if(userBytoken.getId()!=user.getId()){
            return ResultTemplate.fail("-2","非法用户");
        }
        userBytoken.setUsername(user.getUsername());
        userBytoken.setSignature(user.getSignature());
        try{
            userMapper.updateById(userBytoken);
        }catch(Exception e){
            e.printStackTrace();
            return ResultTemplate.fail("修改失败");
        }
        return ResultTemplate.succ("修改成功");
    }
}
